/**
 * 模拟请求模块
 * 替换原来的axios请求，使用本地数据处理
 */

import ExecuteError from "@/utils/executeError";
import { message } from "antd";

// 模拟网络延迟
const delay = (ms = 100) => new Promise(resolve => setTimeout(resolve, ms));

/**
 * 模拟请求函数
 * @param option 模拟的请求参数
 */
export default async function request(option) {
  await delay(); // 模拟网络延迟

  try {
    // 根据URL路由到不同的处理函数
    const { url, method = 'GET', data = {} } = option;

    // 路由到对应的API处理函数
    let result;
    switch (url) {
      case '/user/login':
        result = await import('./mockApi').then(module => module.userLogin(data));
        break;
      case '/user/register':
        result = await import('./mockApi').then(module => module.userRegister(data));
        break;
      case '/folder/openfolder':
        result = await import('./mockApi').then(module => module.openfolder(data));
        break;
      case '/folder/name':
        result = await import('./mockApi').then(module => module.updateName(data));
        break;
      case '/folder/delete':
        result = await import('./mockApi').then(module => module.deleteFolder(data));
        break;
      case '/folder/addfolder':
        result = await import('./mockApi').then(module => module.addFolder(data));
        break;
      case '/folder/sort':
        result = await import('./mockApi').then(module => module.sortUpdateAllFile(data));
        break;
      case '/image/getBackground':
        result = await import('./mockApi').then(module => module.getBackground(data));
        break;
      case '/blog/uploadBase64':
        result = await import('./mockApi').then(module => module.uploadBase64(data));
        break;
      case '/blog/updateContent':
        result = await import('./mockApi').then(module => module.updateContent(data));
        break;
      case '/blog/getBlogContent':
        result = await import('./mockApi').then(module => module.getBlogContent(data));
        break;
      default:
        throw new Error(`未找到API: ${url}`);
    }

    // 处理响应结果
    if (result.status === 200) {
      return result;
    } else {
      message.error(result.message);
      throw new ExecuteError(result.message, result);
    }
  } catch (err) {
    // 其他异常同一化为ExecuteError
    if (!(err instanceof ExecuteError)) {
      console.warn(err);
      throw new ExecuteError(err.message || "操作失败");
    }
    throw err;
  }
}

export async function requestAvatar(option) {
  await delay(); // 模拟网络延迟

  try {
    const { url, method = 'POST', data = {} } = option;

    let result;
    switch (url) {
      case '/user/avatar':
        result = await import('./mockApi').then(module => module.updateAvatar(data));
        break;
      case '/user/updatebackground':
        result = await import('./mockApi').then(module => module.updateBackGroud(data));
        break;
      default:
        throw new Error(`未找到API: ${url}`);
    }

    if (result.status === 200) {
      return result;
    } else {
      message.error(result.message);
      throw new ExecuteError(result.message, result);
    }
  } catch (err) {
    if (!(err instanceof ExecuteError)) {
      console.warn(err);
      throw new ExecuteError(err.message || "操作失败");
    }
    throw err;
  }
}
